package com.whz.parameterized;

import java.util.Collection;
import java.util.EmptyStackException;
import java.util.Iterator;

/**
 * @Author: sinosoft
 * @Description:
 * @Date: Create in 17:06 2021/5/11
 * @Modified By:
 */
public class Stack1<E> implements Iterable<E> {
    private Object[] elements;
    private int size = 0;
    private static final int DEFAULT_INITIAL_CAPACITY = 16;

    /*消除编译时期未受检查警告*/
    public Stack1() {
        /*
        * 无法创建泛型数组 需要创建一个Object数组 并将它转换成泛型数组类型
        * */
        elements = new Object[DEFAULT_INITIAL_CAPACITY];
    }
    public boolean isEmpty() {
        return size == 0;
    }

    public void push(E e) {
        elements[size++] = e;
    }

    public void pushAll(Iterable<? extends E> src) {
        for (E e : src) {
            push(e);
        }
    }
    public E pop() {
        if(size == 0) {
            throw new EmptyStackException();
        }
        @SuppressWarnings("unchecked")
        E result = (E) elements[--size];
        elements[size] = null;
        return result;
    }
    public void popAll(Collection<? super E> dst) {
        while (!isEmpty()) {
            dst.add(pop());
        }
    }

    @Override
    public Iterator<E> iterator() {
        return null;
    }
}
